﻿NAMFox.ns(function() { with (NAMFox) {

//////////////////////////////////////////////
// Helper Methods
//////////////////////////////////////////////

function MockSelector() {
}

MockSelector.prototype = {
    getElements: function() { return []; }
};

var sut = new MarkupStripper(new MockSelector());

function replaceContent(html, lightness) {
    var config = $.getConfig().stripMarkup;

    config.colorLightness = lightness || 0;
    config.stripOptions = StripOptions.Color;

    var div = document.createElement("div");
    div.innerHTML = html;

    sut._replaceContent(div);

    return div.innerHTML;
}

//////////////////////////////////////////////
// Test Cases
//////////////////////////////////////////////

name("Markup Stripper Unit Tests");

// Issue 20

test("Translates One RGB Color", function() {
    var html = '<p><font size="3"><sup><span style="color: rgb(206, 0, 0);">Good job.</span></sup></font><br/><br/></p>';

    var expected = '<p><font size="3"><sup><span style="color: black;">Good job.</span></sup></font><br><br></p>';
    var actual = replaceContent(html);

    equals(expected.escape(), actual.escape(), "The color specified in RGB should turn black.");
});

test("Translates One Named Color", function() {
    var html = '<p><font size="3"><sup><span style="color: white;">Good job.</span></sup></font><br/><br/></p>';
            
    var expected = '<p><font size="3"><sup><span style="color: black;">Good job.</span></sup></font><br><br></p>';
    var actual = replaceContent(html);
    
    equals(expected.escape(), actual.escape(), "The named color should turn black.");
});

test("Translates Nested Colors", function() {
    var html = '<p><span style="color: maroon;">one two three<br/><br/><span style="color: orange;">four five six.<br/></span><br/><br/></span></p>';

    var expected = '<p><span style="color: black;">one two three<br><br><span style="color: black;">four five six.<br></span><br><br></span></p>';
    var actual = replaceContent(html);
    
    equals(expected.escape(), actual.escape(), "Nested colors should all turn black.");
});

test("Ignores Edit Tags", function() {
    var html = '<p><span style="color: maroon;">one two three<br/><br/><span style="color: orange;">four five six.<br/></span><br/><br/></span><span style="color: rgb(238, 240, 249);"><font size="1"><br/><br/>[size=1][color=#666666]This message was edited by Artificer(moderator) on Feb 19 2009.</font></span><font size="1"/></p>';

    var expected = '<p><span style="color: black;">one two three<br><br><span style="color: black;">four five six.<br></span><br><br></span><span style="color: rgb(238, 240, 249);"><font size="1"><br><br>[size=1][color=#666666]This message was edited by Artificer(moderator) on Feb 19 2009.</font></span><font size="1"></font></p>';
    var actual = replaceContent(html);
    
    equals(expected.escape(), actual.escape(), "Color stripping should ignore edit tags.");
});

test("Ignores Multiple Edit Tags", function() {
    var html = '<p><span style="color: maroon;">one two three<br/><br/><span style="color: orange;">four five six.<br/></span><br/><br/></span><span style="color: rgb(238, 240, 249);"><font size="1"><br/><br/><font size="1"><span style="color: rgb(102, 102, 102);">This message was edited by a moderator on Feb 19 2009.</span></font><br/><br/>[size=1][color=#666666]This message was edited by The Great Elf 4 on Feb 19 2009.</font></span><font size="1"/></p>';

    var expected = '<p><span style="color: black;">one two three<br><br><span style="color: black;">four five six.<br></span><br><br></span><span style="color: rgb(238, 240, 249);"><font size="1"><br><br><font size="1"><span style="color: rgb(102, 102, 102);">This message was edited by a moderator on Feb 19 2009.</span></font><br><br>[size=1][color=#666666]This message was edited by The Great Elf 4 on Feb 19 2009.</font></span><font size="1"></font></p>';
    var actual = replaceContent(html);
    
    equals(expected.escape(), actual.escape(), "Color stripping should ignore multiple edit tags.");
});

// Issue 107
test("When lightness preference is 50% colors at 50% (red,  blue) are not stripped", function() {
    var html = '<p><span style="color: red;">one two three</span><span style="color: blue;">one two three</span></p>';
    
    var expected = '<p><span style="color: red;">one two three</span><span style="color: blue;">one two three</span></p>';
    var actual = replaceContent(html, 50);
    
    equals(expected.escape(), actual.escape(), "Color stripping stripped colors at 50% lightness that it should not have.");
});

// Issue 118
test("Colors in quotes are reset to #222266 instead of black.", function() {
    var html = '<blockquote style="color: #222266;"><font size=1>quote <b><a href="/forums/directmessage.php?m=25469247" target="_blank">Highbury Highs</a></b><br /></font><div class="qt"><blockquote style="color: #222266;"><font size=1>quote <b><a href="/forums/directmessage.php?m=25467751" target="_blank">Strawberryclock</a></b><br /></font><div class="qt"><font size=1>No, neo is much better, even though it sucks most of the time.</font><br /></div></blockquote><span style="color:#990000;">Don\'t come here then!<br /></span><br /></div></blockquote>';
    
    var expected = "<blockquote style=\"color: #222266;\"><font size=\"1\">quote <b><a href=\"/forums/directmessage.php?m=25469247\" target=\"_blank\">Highbury Highs</a></b><br></font><div class=\"qt\"><blockquote style=\"color: #222266;\"><font size=\"1\">quote <b><a href=\"/forums/directmessage.php?m=25467751\" target=\"_blank\">Strawberryclock</a></b><br></font><div class=\"qt\"><font size=\"1\">No, neo is much better, even though it sucks most of the time.</font><br></div></blockquote><span style=\"color: rgb(51, 51, 102);\">Don't come here then!<br></span><br></div></blockquote>";
    var expectedOld = "<blockquote style=\"color: rgb(34, 34, 102);\"><font size=\"1\">quote <b><a href=\"/forums/directmessage.php?m=25469247\" target=\"_blank\">Highbury Highs</a></b><br></font><div class=\"qt\"><blockquote style=\"color: rgb(34, 34, 102);\"><font size=\"1\">quote <b><a href=\"/forums/directmessage.php?m=25467751\" target=\"_blank\">Strawberryclock</a></b><br></font><div class=\"qt\"><font size=\"1\">No, neo is much better, even though it sucks most of the time.</font><br></div></blockquote><span style=\"color: rgb(51, 51, 102);\">Don't come here then!<br></span><br></div></blockquote>";
    var actual = replaceContent(html);
    
    if (expected.escape() !== actual.escape()) {
        equals(expectedOld.escape(), actual.escape(), "Colors in [quote] tags should be changed to #222266, not black.");
    }
    else {
        equals(expected.escape(), actual.escape(), "Colors in [quote] tags should be changed to #222266, not black.");
    }
});

}});